//提示框
const alertBox = document.querySelector('.alert');
function showAlert(message,color) {
    alertBox.textContent = message;
    alertBox.style.color = color;
    alertBox.style.display = 'block';
    setTimeout(() => {
        alertBox.style.display = 'none';
    }, 3000);
}

//弹出框
function tc_open2(targetTc,showM,flexOrblock) {
    showM.style.display = "block";
    targetTc.style.display = ""+flexOrblock;
    setTimeout(() => {
        targetTc.style.transform = "scale(1)";
    }, 10);
}
function tc_close2( targetTc,showM) {
    targetTc.style.transform = "scale(0)";
    setTimeout(() => {
        showM.style.display = "none";
        targetTc.style.display = "none";
    }, 100);
}

//confirm
function confirm(title,content){
    const confirmTitle = document.getElementById("confirm-title");
    const confirmContent = document.getElementById("article-content-span");
    confirmTitle.innerHTML = title;
    confirmContent.innerHTML = content;
}

//根据路径确定执行内容
const pathname = window.location.pathname;      // 获取当前页面的路径名
const match = pathname.match(/\/(\d+)$/);       // 使用正则表达式匹配路径名中的id
const numberStr = match[1];
let postid;
let hotOrPost;      // 0 为热度，1 为文章
if (numberStr.startsWith('0')) {
    // 以 0 开头，提取 0 后面的数字
    postid = numberStr.slice(1)-0;
    hotOrPost = 1;
} else {
    postid = numberStr-0;
    hotOrPost = 0;
}
//获取元素
const title = document.getElementById('title');
const articleCover = document.getElementById('article-cover');
const articleTitle = document.getElementById('article-title');
const articleTime = document.getElementById('article-time');
const articleContent = document.getElementById('article-contentP');
const articleTag = document.getElementById('article-tags');
const userAvatar = document.getElementById('user-avatar');
const userName = document.getElementById('user-name');
const userVerifiedInfo = document.getElementById('user-verifiedInfo');
const articleCover2 = document.getElementById('article-cover-none');
function articleData(articleCover1,articleTitle1,articleTime1,articleContent1,articleTag1){     //更新页面文章内容
    if(articleCover1 === null){
        articleCover2.style.display = 'block';
        articleCover.style.display = 'none';
    }else{
        articleCover.src = articleCover1;
    }
    articleTitle.innerHTML = articleTitle1;
    articleTime.innerHTML = articleTime1;
    articleContent.innerHTML = "  "+articleContent1;
    articleTag.innerHTML = `<span className="tag">${articleTag1}</span>`;
}
function userData(userAvatar1,userName1,userVerifiedInfo1){         //更新页面用户信息
    userAvatar.src = userAvatar1;
    userName.innerHTML = userName1;
    userVerifiedInfo.innerHTML = userVerifiedInfo1;
}
function AllData(articleCover1,articleTitle1,articleTime1,articleContent1,articleTag1,userAvatar1,userName1,userVerifiedInfo1){
    if(articleCover1 === null){
        articleCover2.style.display = 'block';
        articleCover.style.display = 'none';
    }else{
        articleCover.src = articleCover1;
    }
    articleTitle.innerHTML = articleTitle1;
    articleTime.innerHTML = articleTime1;
    articleContent.innerHTML = "  "+articleContent1;
    articleTag.innerHTML = `<span className="tag">${articleTag1}</span>`;
    userAvatar.src = userAvatar1;
    userName.innerHTML = userName1;
    userVerifiedInfo.innerHTML = userVerifiedInfo1;
}
function getArticleData(url) {          //获取文章信息
    $.ajax({
        url: url+postid,
        type: 'GET',
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
            articleData(data.coverImage,data.title,data.createdTime,data.content,data.tagName);     //更新页面文章内容
            getUserData(data.userId);
        },
        error: function (xhr, status, error) {
            showAlert('获取文章信息失败','red');
        }
    })
}
function getUserData(userId) {          //获取用户信息
    let verificationInfo;
    $.ajax({
        url: '/AdminController/users/'+userId,
        type: 'GET',
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
            verificationInfo= data.verificationInfo===null?"未认证":data.verificationInfo;
            userData(data.avatar,data.userName,verificationInfo);
        },
        error: function () {
           showAlert('获取用户信息失败', 'red');
        }
    })
}
const btns = document.getElementById('article-actions');        //用于更换按钮
const confirmTc = document.getElementById('confirm-container');     //confirm弹窗
const confirmBtn = document.getElementById('confirm-button');
const cancelBtn = document.getElementById('cancel-button');
const confirmShowmodal = document.getElementById('confirmShowmodal');
if(hotOrPost===1){
    getArticleData('/AdminController/hotSearch/');
    btns.innerHTML = `
        <div class="action-buttons">
        <button id="btn-limit">
          <i class="fa fa-tachometer"></i> 通过
        </button>
        <button id="btn-block">
          <i class="fa fa-ban"></i> 驳回
        </button>
        <button id="btn-back" onclick="window.history.back()">
          <i class="fa fa-arrow-left"></i> 返回
        </button>
      </div>
    `;
    const btn2 = document.getElementById('btn-block');
    btn1.onclick = () => {
        confirm("审核-通过","是否通过该文章审核？")
        tc_open2(confirmTc,confirmShowmodal,'block');
        confirmBtn.onclick = () => {
            $.ajax({
                url: '/AdminController/audit/approve/'+postid,
                type: 'post',
                dataType: 'text',
                contentType: 'application/json',
                success: function (data) {
                    showAlert('通过成功','green');
                    tc_close2(confirmTc,confirmShowmodal);
                },
                error: function () {
                    showAlert('通过失败,请求超时','red');
                }
            })
        };
        [cancelBtn,confirmShowmodal].forEach(btn => {
            btn.onclick = () => {
                tc_close2(confirmTc,confirmShowmodal);
            }
        })
    }
    btn2.onclick = () => {
        confirm("审核-驳回","是否驳回该文章审核？")
        tc_open2(confirmTc,confirmShowmodal,'block');
        confirmBtn.onclick = () => {
            $.ajax({
                url: '/AdminController/audit/reject/'+postid,
                type: 'post',
                dataType: 'text',
                contentType: 'application/json',
                success: function (data) {
                    showAlert('驳回成功','green');
                    tc_close2(confirmTc,confirmShowmodal);
                },
                error: function () {
                    showAlert('驳回失败,请求超时','red');
                }
            })
        }
        [cancelBtn,confirmShowmodal].forEach(btn => {
            btn.onclick = () => {
                tc_close2(confirmTc,confirmShowmodal);
            }
        })
    }
}else if(hotOrPost===0){
    $.ajax({
        url: '/AdminController/hotSearch/hot/' + postid,
        type: 'GET',
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
            btns.innerHTML = `
                <div class="action-buttons">
                    <button id="btn-save">
                      <i class="fa"></i> 保存
                    </button>
                    <button id="btn-block">
                      <i class="fa fa-ban"></i> <span id="isblock">${data.isBlock===1?'解除屏蔽':'屏蔽'}</span> 
                    </button>
                    <button id="btn-back" onclick="window.history.back()">
                      <i class="fa fa-arrow-left"></i> 返回
                    </button>
              </div>
            `;
            AllData(data.postCover,data.title,data.createdTime,data.content,data.tagName,data.avatar,data.authorName,data.verContent);
            const btn = document.getElementById('btn-save');
            const btn1 = document.getElementById('btn-limit');
            const btn2 = document.getElementById('btn-block');
            const islimit = document.getElementById('islimit');
            const isblock = document.getElementById('isblock');
            btn.onclick = () => {
                const articleTitle = document.getElementById('article-title');
                const articleContent = document.getElementById('article-contentP');
                let Title = articleTitle.textContent;
                let Content = articleContent.textContent;
                confirm("保存","是否保存对该热搜的修改？")
                tc_open2(confirmTc,confirmShowmodal,'block');
                confirmBtn.onclick = () => {
                    $.ajax({
                        url: `/AdminController/hotSearch/update/${postid}?content=${Content}&title=${Title}`,
                        type: 'post',
                        dataType: 'text',
                        contentType: 'application/json',
                        success: function () {
                            showAlert('保存成功','green');
                            tc_close2(confirmTc,confirmShowmodal);
                        },
                        error: function () {
                            showAlert('保存失败,请求超时','red');
                        }
                    })
                }
                [cancelBtn,confirmShowmodal].forEach(btn => {
                    btn.onclick = () => {
                        tc_close2(confirmTc,confirmShowmodal);
                    }
                })
            }
            // btn1.onclick = () => {
            //     let a = islimit.innerHTML === '限流';
            //     if(a)confirm("限流","是否对该热搜进行热搜？")
            //     else confirm("取消限流","是否对该热搜取消限流？")
            //     tc_open2(confirmTc,confirmShowmodal,'block');
            //     confirmBtn.onclick = () => {
            //         $.ajax({
            //             url: `/AdminController/hotSearch/limit/${postid}?enable=${a}`,
            //             type: 'post',
            //             dataType: 'text',
            //             contentType: 'application/json',
            //             success: function () {
            //                 showAlert('限流成功','green');
            //                 if(a)islimit.innerHTML = '解除限流';
            //                 else islimit.innerHTML = '限流';
            //                 tc_close2(confirmTc,confirmShowmodal);
            //             },
            //             error: function () {
            //                 showAlert('限流失败,请求超时','red');
            //             }
            //         })
            //     }
            //     [cancelBtn,confirmShowmodal].forEach(btn => {
            //         btn.onclick = () => {
            //             tc_close2(confirmTc,confirmShowmodal);
            //         }
            //     })
            // }
            btn2.onclick = () => {
                let a = isblock.innerHTML === '屏蔽';
                if(a)confirm("屏蔽","是否对该热搜进行屏蔽？")
                else confirm("取消屏蔽","是否对该热搜取消屏蔽？")
                tc_open2(confirmTc,confirmShowmodal,'block');        //confirm弹窗
                confirmBtn.onclick = () => {
                    $.ajax({
                        url: `/AdminController/hotSearch/block/${postid}?enable=${a}`,
                        type: 'post',
                        dataType: 'text',
                        contentType: 'application/json',
                        success: function () {
                            showAlert(`${a?'屏蔽成功':'取消屏蔽成功'}`,'green');
                            if(a)isblock.innerHTML = '解除屏蔽';
                            else isblock.innerHTML = '屏蔽';
                            tc_close2(confirmTc,confirmShowmodal);
                        },
                        error: function () {
                            showAlert(`${a?'屏蔽失败,请求超时':'取消屏蔽失败,请求超时'}`,'red');
                        }
                    })
                }
                [cancelBtn,confirmShowmodal].forEach(btn => {
                    btn.onclick = () => {
                        tc_close2(confirmTc,confirmShowmodal);
                    }
                })
            }
        },
        error: function () {
            showAlert('获取热度信息失败', 'red');
        }
    });
}